package com.alan.api.util;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * Created by Alan on 17-10-13
 */
@Component
public class RedisUtils {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private String pre_key = "ruijian_api_";

    public String get(String key){
        return stringRedisTemplate.opsForValue().get( pre_key + key );
    }

    public <T> T getObject(String key,Class<T> clazz){
        String json = stringRedisTemplate.opsForValue().get( pre_key + key );
        return JSON.parseObject(json,clazz);
    }

    public void set(String key,String value){
        stringRedisTemplate.opsForValue().set( pre_key + key ,value);
    }

    public void set(String key,String value,Long timeOut){
        stringRedisTemplate.opsForValue().set( pre_key + key , value, timeOut, TimeUnit.SECONDS);
    }

    public void setObject(String key,Object value, Long timeOut){
        stringRedisTemplate.opsForValue().set( pre_key + key , JSON.toJSONString(value), timeOut, TimeUnit.SECONDS);
    }

    public void del(String key){
        stringRedisTemplate.delete( pre_key + key );
    }


}
